package com.shtel.bss.saas.tenant.service.impl;

import com.github.pagehelper.PageInfo;
import com.shtel.bss.saas.core.data.common.service.DataServiceImpl;
import com.shtel.bss.saas.tenant.bo.SysGlobalAuthBo;
import com.shtel.bss.saas.tenant.entity.SysGlobalAuth;
import com.shtel.bss.saas.tenant.entity.SysTenantAuthJoin;
import com.shtel.bss.saas.tenant.manager.SysGlobalAuthManager;
import com.shtel.bss.saas.tenant.manager.SysTenantAuthJoinManager;
import com.shtel.bss.saas.tenant.mapstrut.EntityDtoMapper;
import com.shtel.bss.saas.tenant.service.ISysTenantAuthJoinService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 租户权限表服务
 * @author xiezhiwei 2020-02-05 22:02
 */
@Service("SysTenantAuthJoinService")
public class SysTenantAuthJoinServiceImpl extends DataServiceImpl implements ISysTenantAuthJoinService {

    @Override
    public void updateSysTenantAuth(List<Long> authIds, Long tenantId,Long createUserId) {
        SysTenantAuthJoin sysTenantAuthJoin = new SysTenantAuthJoin();
        sysTenantAuthJoin.setTenantId(tenantId);
        //查出该租户对应的所有权限
        List<SysTenantAuthJoin> sysTenantAuthJoinList =
                SysTenantAuthJoinManager.selectEntity(sysTenantAuthJoin);
        Map<Long,SysTenantAuthJoin> authIdMap =  this.tranSysTenantAuthJoinList2Map(sysTenantAuthJoinList);
        for(Long authId : authIds){
            //已存在的权限  不做处理
            if(authIdMap.get(authId)!=null){
                authIdMap.remove(authId);
                continue;
            }
            //不存在的权限  新增
            sysTenantAuthJoin.setAuthId(authId);
            sysTenantAuthJoin.setCreateUserId(createUserId);
            sysTenantAuthJoin.setCreateTime(new Date());
            SysTenantAuthJoinManager.saveEntity(sysTenantAuthJoin);
        }
        //多余的权限  删除
        Iterator<Map.Entry<Long, SysTenantAuthJoin>> it=authIdMap.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<Long, SysTenantAuthJoin> entry = it.next();
            if (entry == null) {
                continue;
            }
            SysTenantAuthJoin sysTenantAuthJoin1 = entry.getValue();
            if (sysTenantAuthJoin1 == null) {
                continue;
            }
            SysTenantAuthJoinManager.removeEntity(sysTenantAuthJoin1);
        }
    }
    private Map<Long, SysTenantAuthJoin> tranSysTenantAuthJoinList2Map(List<SysTenantAuthJoin> list) {
        Map<Long, SysTenantAuthJoin> map = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return map;
        }
        for (SysTenantAuthJoin sysTenantAuthJoin : list) {
            map.put(sysTenantAuthJoin.getAuthId(), sysTenantAuthJoin);
        }
        return map;
    }

    @Override
    public PageInfo<SysGlobalAuthBo> querySysGlobalAuthListByTenantId(Long tenantId, int pageSize, int pageNum) {

        return EntityDtoMapper.INSTANCE.
                toSysGlobalAuthPageInfo(SysGlobalAuthManager.selectEntityByTenantId( tenantId,pageSize,pageNum));
    }

    @Override
    public List<SysGlobalAuthBo> querySysGlobalAuthListByTenantId(Long tenantId) {
        return EntityDtoMapper.INSTANCE.
                toSysGlobalAuth(SysGlobalAuthManager.selectEntityByTenantId(tenantId));
    }

    @Override
    @Transactional
    public void deleteSysTenantAuth(List<Long>  tenantIds, Long authId) {
        if(tenantIds != null && !tenantIds.isEmpty()){
            for (Long tenantId : tenantIds) {
                SysTenantAuthJoin sysTenantAuthJoin = new SysTenantAuthJoin();
                sysTenantAuthJoin.setAuthId(authId);
                sysTenantAuthJoin.setTenantId(tenantId);
                SysTenantAuthJoinManager.removeEntity(sysTenantAuthJoin);
            }
        }

    }
}